package epoint.com.helloworld;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import com.esri.android.map.LocationDisplayManager;
import com.esri.android.map.MapOnTouchListener;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.android.runtime.ArcGISRuntime;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.LinearUnit;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.Unit;

/**
 * 作者： andli
 * 时间： 2017/11/18 14:28
 * 描述： GPS定位的实现
 */
public class MainActivity extends AppCompatActivity {

    MapView mMapView;

    LocationDisplayManager locationDisplayManager;

    Button gpsButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        ArcGISRuntime.setClientId("L8zvYbPtXBtrHJ26");

        mMapView = (MapView) findViewById(R.id.mapview);
        gpsButton = (Button) findViewById(R.id.gpsbutton);

        gpsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 手动刷新当前位置
                if (mMapView.isLoaded()) {
                    if ((locationDisplayManager != null) && (locationDisplayManager.getLocation() != null)) {
                        locationDisplayManager.setAutoPanMode(LocationDisplayManager.AutoPanMode.LOCATION);
                    }
                }
            }
        });

        // 添加底图
        String url = "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer";
        ArcGISTiledMapServiceLayer tiledMapServiceLayer = new ArcGISTiledMapServiceLayer(url);
        mMapView.addLayer(tiledMapServiceLayer);

        mMapView.setOnStatusChangedListener(new OnStatusChangedListener() {
            @Override
            public void onStatusChanged(Object o, STATUS status) {
                if (o == mMapView && status == STATUS.INITIALIZED) {
                    if (locationDisplayManager == null) {
                        // 开启定位
                        setupLocationListener();
                    }
                }
            }
        });

        mMapView.setOnTouchListener(new MyTouchListener(this, mMapView));

    }

    // 监听位置变化
    private void setupLocationListener() {
        if ((mMapView != null) && (mMapView.isLoaded())) {

            locationDisplayManager = mMapView.getLocationDisplayManager();
            locationDisplayManager.setLocationListener(new LocationListener() {

                boolean locationChanged = false;

                // Zooms to the current location when first GPS fix arrives.
                @Override
                public void onLocationChanged(Location loc) {
                    if (!locationChanged) {
                        locationChanged = true;
                        // 缩放到指定位置
                        zoomToLocation(loc);
                        // 设置定位模式
                        locationDisplayManager.setAutoPanMode(LocationDisplayManager.AutoPanMode.LOCATION);
                    }
                }

                @Override
                public void onProviderDisabled(String arg0) {
                }

                @Override
                public void onProviderEnabled(String arg0) {
                }

                @Override
                public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
                }
            });

            locationDisplayManager.start();
        }
    }

    // 缩放级别
    final static double ZOOM_BY = 20;

    private void zoomToLocation(Location loc) {
        Point mapPoint = getAsPoint(loc);
        // 缩放到位置点
        // mMapView.zoomTo(mapPoint,2000);

        // 缩放到指定区域
        Unit mapUnit = mMapView.getSpatialReference().getUnit();
        double zoomFactor = Unit.convertUnits(ZOOM_BY, Unit.create(LinearUnit.Code.MILE_US), mapUnit);

        Envelope zoomExtent = new Envelope(mapPoint, zoomFactor, zoomFactor);
        mMapView.setExtent(zoomExtent);
    }

    // GPS坐标 转 Point
    private Point getAsPoint(Location loc) {
        Point wgsPoint = new Point(loc.getLongitude(), loc.getLatitude());
        return (Point) GeometryEngine.project(wgsPoint, mMapView.getSpatialReference(), mMapView.getSpatialReference());
    }

    // 地图触摸事件
    class MyTouchListener extends MapOnTouchListener {

        public MyTouchListener(Context context, MapView view) {
            super(context, view);
        }

        //单击事件
        public boolean onSingleTap(MotionEvent e) {
            Log.i("andli", "当前地图比例: " + mMapView.getScale());
            return true;

        }

        public boolean onDragPointerMove(MotionEvent from, MotionEvent to) {
            return super.onDragPointerMove(from, to);
        }

        @Override
        public boolean onDragPointerUp(MotionEvent from, MotionEvent to) {

            return super.onDragPointerUp(from, to);
        }

    }
}

